﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcVehicleInsurance.Models;

namespace MvcVehicleInsurance.Areas.Admin.Controllers
{
    public class ManufacController : Controller
    {
        private DataClassesVehicleDataContext data = new DataClassesVehicleDataContext();

        //
        // GET: /Admin/Manufac/
    

        public ActionResult Index()
        {
            List<MvcVehicleInsurance.Models.Manufacturer> listmanu = (from manu in data.Manufacturers select manu).ToList();
            if (listmanu.Count > 0)
            {
                return View(listmanu);
            }

            return View();
        }

        //
        // GET: /Admin/Manufac/Details/5

        public ActionResult Details(int id)
        {
            Manufacturer details_mn = data.Manufacturers.Where(x => x.ManufacturerId == id).First();

            if (ModelState.IsValid)
            {
                return View(details_mn);
            }

            return View();
        }

        //
        // GET: /Admin/Manufac/Create

        public ActionResult Create()
        {

            return View();
        }

        //
        // POST: /Admin/Manufac/Create

        [HttpPost]
        public ActionResult Create(Manufacturer collection, [Bind(Exclude = "ManufacturerId")] Manufacturer mn)
        {

            if (ModelState.IsValid)
            {
                data.Manufacturers.InsertOnSubmit(collection);
                data.SubmitChanges();
            }

            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Admin/Manufac/Edit/5

        public ActionResult Edit(int id)
        {
            Manufacturer edit_mn = data.Manufacturers.First(x => x.ManufacturerId == id);
            return View(edit_mn);
        }

        //
        // POST: /Admin/Manufac/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, string name)
        {
            Manufacturer edit_mn = data.Manufacturers.First(x => x.ManufacturerId == id);
            if (ModelState.IsValid)
            {
                edit_mn.ManufacturerId = id;
                edit_mn.Name = name;
                data.SubmitChanges();
            }

            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int i)
        {
            Manufacturer edit_mn = data.Manufacturers.First(x => x.ManufacturerId == i);
            if (ModelState.IsValid)
            {
                data.Manufacturers.DeleteOnSubmit(edit_mn);
                data.SubmitChanges();
            }
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

    }
}
